﻿using Lan.Infrastructure.CameraOnvif;
using Lan.ServiceCore.Services;
using Model;
using SqlSugar.IOC;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;

namespace Lan.ServiceCore.Onvif
{
    public class RBTRACK_Info
    {
        public int channelId { get; set; }
        public string DefenceareaId { get; set; }
        public string CameraIp { get; set; }
    }
    public class RBTRACKManage
    {
        ConcurrentDictionary<string, RBTRACK_Info> _dictRBTRACKO;

        public static List<RBTRACK_Info> list_RBTRACK = new List<RBTRACK_Info>();

        private static RBTRACKManage _instance = null;
        public static RBTRACKManage GetInstance()
        {
            return _instance;
        }
        public static void Init()
        {
            if (list_RBTRACK.Count > 0)
            {
                //Program.m_enTrack = 1;
                for (int i = 0; i < list_RBTRACK.Count; i++)
                {
                    RBTrackSdk.RBTRACK_DeleteTrack(list_RBTRACK[i].channelId);
                }
                list_RBTRACK = new List<RBTRACK_Info>();
                RBTrackSdk.RBTRACK_DeInit();
                _instance = null;
            }

            if (_instance == null)
                _instance = new RBTRACKManage();

            if (list_RBTRACK.Count <= 0)
            {
                _instance = new RBTRACKManage();
            }
        }

        private RBTrackSdk.TrackCallBack _TrackCallBack;

        private RBTRACKManage()
        {
            _TrackCallBack = TrackCallBack;

            _dictRBTRACKO = LoadONVIF_RBTRACK();
        }

        private ConcurrentDictionary<string, RBTRACK_Info> LoadONVIF_RBTRACK()
        {

            if (list_RBTRACK.Count > 0)
            {
                //Program.m_enTrack = 1;
                for (int i = 0; i < list_RBTRACK.Count; i++)
                {
                    RBTrackSdk.RBTRACK_DeleteTrack(list_RBTRACK[i].channelId);
                }
                RBTrackSdk.RBTRACK_DeInit();
            }

            CameraService cameraService = new CameraService();
            List<CameraModel> cameraBuffers = cameraService.GetAllList();

            Dictionary<string, RBTRACK_Info> dic;

            if (cameraBuffers == null)
                dic = new Dictionary<string, RBTRACK_Info>();
            else
            {
                dic = new Dictionary<string, RBTRACK_Info>(cameraBuffers.Count);
                //Program.m_enTrack = 0;
                int ret = RBTrackSdk.RBTRACK_Init(256);

                foreach (CameraModel item in cameraBuffers)
                {
                    if (!string.IsNullOrEmpty(item.CameraURL))
                    {
                        ONVIF_ANGLE_MAP_T panLeftToMiddle = new ONVIF_ANGLE_MAP_T();
                        panLeftToMiddle.fromOnvifVal = item.PanLeft; //从ONVIF值
                        panLeftToMiddle.toOnvifVal = item.PanMiddle;  //到ONVIF值
                        panLeftToMiddle.fromAngle = item.PanLeftAngle;	//从球机角度
                        panLeftToMiddle.toAngle = item.PanMiddleAngle;   //到球机角度

                        ONVIF_ANGLE_MAP_T panMiddleToRight = new ONVIF_ANGLE_MAP_T();
                        panMiddleToRight.fromOnvifVal = item.PanMiddle2; //从ONVIF值
                        panMiddleToRight.toOnvifVal = item.PanRight;  //到ONVIF值
                        panMiddleToRight.fromAngle = item.PanMiddle2Angle;	//从球机角度
                        panMiddleToRight.toAngle = item.PanRightAngle;   //到球机角度  float.Parse(Camera.tiltTopToBottom.Split(',')[1]);   //到球机角度

                        ONVIF_ANGLE_MAP_T tiltTopToBottom = new ONVIF_ANGLE_MAP_T();
                        tiltTopToBottom.fromOnvifVal = item.TiltTop; //从ONVIF值
                        tiltTopToBottom.toOnvifVal = item.TiltBottom;  //到ONVIF值
                        tiltTopToBottom.fromAngle = item.TiltTopAngle;	//从球机角度
                        tiltTopToBottom.toAngle = item.TiltBottomAngle;

                        BALL_PARA_T _BALL_PARA_T = new BALL_PARA_T();
                        _BALL_PARA_T.trackMode = item.TrackMode;
                        _BALL_PARA_T.viewAngle1X = item.MinViewAngle;
                        _BALL_PARA_T.viewAngle2X = item.ViewAngle2X;
                        _BALL_PARA_T.viewAngleMaxX = item.MaxViewAngle;
                        _BALL_PARA_T.maxZoom = item.MaxZoom;
                        _BALL_PARA_T.maxZoomRatio = 1920.0f * 0.5f / item.MaxZoomRatio;// ;
                        _BALL_PARA_T.panCounterclockwise = item.Counterclockwise;
                        _BALL_PARA_T.panLeftToMiddle = panLeftToMiddle;
                        _BALL_PARA_T.panMiddleToRight = panMiddleToRight;
                        _BALL_PARA_T.tiltTopToBottom = tiltTopToBottom;

                        _BALL_PARA_T.panKp = 0.0f;
                        _BALL_PARA_T.panKi = 0.0f;
                        _BALL_PARA_T.tiltKp = 0.0f;
                        _BALL_PARA_T.tiltKi = 0.0f;
                        ////防区雷达=相机绑定雷达

                        CalibrationService calibrationService = new CalibrationService();

                        Calibration calibration = calibrationService.GetInfo(item.Ip, item.BindingAreaId);

                        if (calibration != null)
                        {
                            //如果是主雷达
                            _BALL_PARA_T.ballPointX = calibration.CameraPointX;
                            _BALL_PARA_T.ballPointY = calibration.CameraPointY;

                            _BALL_PARA_T.referPointX = 0f;
                            _BALL_PARA_T.referPointY = calibration.CalibrationDistance; //30f;
                            _BALL_PARA_T.referPointPan = calibration.CamerarPointAngle;

                            _BALL_PARA_T.minZoomPan = item.MinZoomPan;
                            _BALL_PARA_T.minZoomTilt = item.MinZoomTilt;
                            _BALL_PARA_T.maxZoomPan = item.MaxZoomPan;
                            _BALL_PARA_T.maxZoomTilt = item.MaxZoomTilt;

                        }
                        else
                        {
                            _BALL_PARA_T.ballPointX = 0f;
                            _BALL_PARA_T.ballPointY = 0f;
                            _BALL_PARA_T.referPointX = 0f;
                            _BALL_PARA_T.referPointY = 30f;
                            _BALL_PARA_T.referPointPan = 0f;
                            _BALL_PARA_T.minZoomPan = 0;
                            _BALL_PARA_T.minZoomTilt = 0;
                            _BALL_PARA_T.maxZoomPan = 0;
                            _BALL_PARA_T.maxZoomTilt = 0;
                            continue;
                        }

                        _BALL_PARA_T.installHeight = item.CameraHeight - 0.5f;

                        int nSizeOfPerson = Marshal.SizeOf(_BALL_PARA_T);                 //定义指针长度
                        IntPtr Intprt_common = Marshal.AllocHGlobal(nSizeOfPerson);        //定义指针
                        Marshal.StructureToPtr(_BALL_PARA_T, Intprt_common, true);                //将结构体转为指针

                        int channelId = RBTrackSdk.RBTRACK_CreateTrack(item.Ip, item.Username, item.Password, ref _BALL_PARA_T, _TrackCallBack, IntPtr.Zero);
                        Marshal.FreeHGlobal(Intprt_common);
                        if (channelId != -1)
                        {
                            //设置跟踪模式
                            int s = RBTrackSdk.RBTRACK_SetTrackMode(channelId, eTrackMode.eTrack_Distance_Priority);

                            RBTRACK_Info ri = new RBTRACK_Info();
                            ri.channelId = channelId;
                            ri.CameraIp = item.Ip;
                            ri.DefenceareaId = item.BindingAreaId.ToString();

                            list_RBTRACK.Add(ri);
                        }
                    }
                }
            }
            return new ConcurrentDictionary<string, RBTRACK_Info>(dic);
        }
        private int TrackCallBack(int channelId, RADAR_TARGETS_T target, IntPtr userPtr)
        {
            try
            {
                //TargetQueue.AddTrackTarget(target.targetId, target.type,target.cod_X, target.cod_Y,target.speed_Y);
            }
            catch
            {
            }
            return 0;
        }
    }
}
